/*
 * @Author: cest cestrmail@163.com
 * @Date: 2022-12-12 09:35:08
 * @LastEditors: cest cestrmail@163.com
 * @LastEditTime: 2022-12-12 16:01:50
 * @FilePath: /ithome-lite/src/utils/index.ts
 * @Description: 介绍文件的作用、文件的入参、出参。
 */
export const platform = process.env.VUE_APP_PLATFORM

/**
 * Slide 格式化
 * @param slide
 * @returns
 */
export function formatSlideList(slide: Models.News): Models.Slide {
  const title = slide.title['#text']
  const image = slide.image['#text']
  const link = slide.link['#text']
  return {
    title,
    image,
    link: `/pages/detail/news?id=${link}&title=${title}`
  }
}

/**
 * NewsItem 格式化
 * @param news
 * @returns
 */
export function formatNewsList(news: Models.NewsContent): ComponentModels.NewsItem {
  const { newsid, title, postdate, commentcount, image } = news
  return {
    id: newsid,
    title,
    postdate: formatTime(postdate),
    commentcount,
    // lapinid,
    image,
    link: `/pages/detail/news?id=${newsid}&title=${title}`
  }
}

/**
 * 时间格式化
 * @param date
 * @returns
 */
export function formatTime(date: string | number | Date) {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

/**
 * 数字格式化 不满两位数，则补 0，否则不做处理，原样返回
 * @example 例如：
 * formatNumber(6) //=> 06
 * formatNumber(16) //=> 16
 * @param {number} n
 * @returns {string}
 */
function formatNumber(n: number): string {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}
